import React from 'react'
import { withRouter } from 'react-router-dom'
import { connect } from 'react-redux'
import axios from 'axios'
import { save } from '@/redux/User.redux'
import { receiveMsg, getMessages } from '@/redux/Chat.redux'

@withRouter
@connect(
    null,
    { save, receiveMsg, getMessages }
)
class Authorize extends React.Component{

    componentDidMount(){
        this.props.receiveMsg()
        //this.props.getMessages()
        const publicUrl = ['/login', '/register']
        if(publicUrl.includes(this.props.location.pathname)) {
            return
        }
        axios.get('/user/auth')
            .then( res => {
                const { code } = res.data
                if(code === 0){
                    this.props.save(res.data.user)
                }else{
                    //this.props.history.push('/login')
                }
            })
    }

    render(){
        return null
    }
}

export default Authorize
